// Sage Android Client.
// Connect to a Sage server, remotely execute commands and display results.
//
// Copyright (C) 2010, Harald Schilly <harald.schilly@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package org.sagemath.android.interacts;

import org.sagemath.android.Calculation;
import org.sagemath.android.R;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * A simple interact that executes the given lines of code.
 * 
 * @author schilly
 */
public class ExecuteCommand extends AbstractInteract {
  private TextView output;
  
  public ExecuteCommand() {
    super(R.layout.execute_command);
  }
  
  /*
   * (non-Javadoc)
   * @see
   * org.sagemath.android.interacts.AbstractInteract#build()
   */
  @Override
  protected void init() {
    
    final EditText input = (EditText) myView.findViewById(R.id.exInputText);
    output = (TextView) myView.findViewById(R.id.exOutput);
    final Button button = (Button) myView.findViewById(R.id.exSend);
    
    button.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        calculate(input.getText().toString());
      }
    });
    
  }
  
  @Override
  public void onResult(Calculation c) {
    // interpret output and update UI
    output.setText(c.getResult());
  }
  
  /*
   * (non-Javadoc)
   * @see
   * org.sagemath.android.interacts.AbstractInteract#onError(java.lang.String)
   */
  @Override
  protected void onError(String msg) {
    output.setText("Error: " + msg);
  }
  
  /*
   * (non-Javadoc)
   * @see org.sagemath.android.interacts.AbstractInteract#reset()
   */
  @Override
  public void reset() {
    output.setText("");
  }
}
